<?php
class ModelCatalogCategory extends Model {
	public function getCategory($category_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1'");
		
		return $query->row;
	}
	
	public function getCategories($parent_id = 0) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'  AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");
		
		return $query->rows;
	}

	public function getCategoriesByParentId($category_id) {
		$category_data = array();
		
		$category_query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "category WHERE parent_id = '" . (int)$category_id . "'");
		
		foreach ($category_query->rows as $category) {
			$category_data[] = $category['category_id'];
			
			$children = $this->getCategoriesByParentId($category['category_id']);
			
			if ($children) {
				$category_data = array_merge($children, $category_data);
			}			
		}
		
		return $category_data;
	}
		
	public function getCategoryLayoutId($category_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_to_layout WHERE category_id = '" . (int)$category_id . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "'");
		
		if ($query->num_rows) {
			return $query->row['layout_id'];
		} else {
			return $this->config->get('config_layout_category');
		}
	}
					
	public function getTotalCategoriesByCategoryId($parent_id = 0) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1'");
		
		return $query->row['total'];
	}
	
	public function getAllCategories($parent_id = 0) {
		$categories1 = array();
		$categories = $this->model_catalog_category->getCategories($parent_id);
		foreach ($categories as $category) {
			$children_data = array();
			
			$children = $this->getCategories($category['category_id']);
			
			foreach ($children as $child) {
				$children_data[] = array(
					'category_id' => $child['category_id'],
					'name'        => $child['name'],
					'image'       => $category['image'],
					'href'        => $this->url->link('product/category', 'path=' . $this->getPath($child['category_id']))	
					
					//'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])	
				);					
			}
			
			/*
			if($parent_id>0) {
				$path = 'path=' . $parent_id.'_'.$category['category_id'] ;
			} else {
				$path = 'path=' . $category['category_id'] ;
			}
			*/
					
			//if ($children_data) {
				//$children_data[]= array_merge($children_data, $this->getAllCategories($category['category_id']));
			//}	
			$categories1[] = array(
				'category_id' => $category['category_id'],
				'name'        => $category['name'],
				'image'       => $category['image'],
				'children'    => $children_data,
				'href'        => $this->url->link('product/category', 'path=' .$this->getPath($category['category_id']))
			);
			//$categories1[][]= array_merge($categories1, $this->getAllCategories($category['category_id']));
			
		}
		return $categories1;
	}
	public function getPathParent($category_id) {
		$query = $this->db->query("SELECT name, parent_id FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC");
		
		if ($query->row['parent_id']) {
			//return $this->getPath($query->row['parent_id'], $this->config->get('config_language_id')) . $this->language->get('text_separator') . $query->row['name'];
			return $this->getPathParent($query->row['parent_id'], $this->config->get('config_language_id'));
		} else {
			return $query->row['name'];
		}
	}
	
	public function getPath($category_id) {
		$query = $this->db->query("SELECT name, parent_id,c.category_id FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC");
		
		if ($query->row['parent_id']) {
			return $this->getPath($query->row['parent_id']) . '_' . $query->row['category_id'];
			
		} else {
			return $query->row['category_id'];
		}
	}
	
	public function getFullPath($category_id) {
		$query = $this->db->query("SELECT name, parent_id,c.category_id FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC");
		
		if ($query->row['parent_id']) {
			//return $this->getPath($query->row['parent_id'], $this->config->get('config_language_id')) . $this->language->get('text_separator') . $query->row['name'];
			//$tmp = $this->getFullPath($query->row['parent_id']) ;
			$tmp[] = array("name"=>$query->row['name'],"href"=>$query->row['parent_id'].'_'.$query->row['category_id']);
			
			return $tmp;
		} else {
			$tmp[] = array("name"=>$query->row['name'],"href"=>$query->row['category_id']);
			
			return $tmp;
			//return array("name"=>$query->row['name'],"href"=>$query->row['category_id']);
		}
	}
	
	
	public function getAllCategoriesHtml($parent_id = 0) {
		$categories1 = array();
		$categories = $this->model_catalog_category->getCategories($parent_id);
		foreach ($categories as $category) {
			$children_data = array();
			
			$categories1[] = array(
				'category_id' => $category['category_id'],
				'name'        => $category['name'],
				'image'       => $category['image'],
				'child' => $this->getAllCategoriesHtml($category['category_id']),
				'href'        => $this->url->link('product/category', 'path=' .$this->getPath($category['category_id']))
			);
			//$categories1[][]= array_merge($categories1, $this->getAllCategories($category['category_id']));
			
		}
		return $categories1;
	}
	
	
}
?>